package com.ccrfid.rmc6.service.device;

import com.ccrfid.rmc6.entity.device.CardEntity;
import com.ccrfid.rmc6.pojo.amq.AmqCard;
import com.ccrfid.rmc6.service.amq.MqMsgProducer;
import com.ccrfid.rmc6.util.JsonUtil;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Aspect
@Component
public class CardServiceAspect {
    private static final Logger logger = LoggerFactory.getLogger(CardServiceAspect.class);
    @Autowired
    private MqMsgProducer mqMsgProducer;

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.device.CardService.createCard(..))",
            returning = "ret")
    public void afterCreateCard(CardEntity ret) {
        AmqCard msgCard = new AmqCard(ret.getId(), ret.getCardNumber(), ret.getCardType(), ret.getEnabled());
        String msg = JsonUtil.INSTANCE.writeValueAsString(msgCard);
        mqMsgProducer.send2Topic("ws_card_add", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.device.CardService.updateCard(Long, com.ccrfid.rmc6.entity.device.CardEntity))",
            returning = "ret")
    public void afterUpdateCard(CardEntity ret) {
        AmqCard msgCard = new AmqCard(ret.getId(), ret.getCardNumber(), ret.getCardType(), ret.getEnabled());
        String msg = JsonUtil.INSTANCE.writeValueAsString(msgCard);
        mqMsgProducer.send2Topic("ws_card_update", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.device.CardService.deleteCards(..)) && args(idList)",
            argNames = "idList")
    public void afterDeleteCards(List<Long> idList) {
        String msg = JsonUtil.INSTANCE.writeValueAsString(idList);
        mqMsgProducer.send2Topic("ws_card_delete", msg);
    }
}
